package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.FoodMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.FoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@Transactional
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //添加菜品
    @Override
    public void addFood(DishDTO dishDTO) {
        //向菜品中插入数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        LocalDateTime now = LocalDateTime.now();
        dish.setCreateTime(now);
        dish.setUpdateTime(now);
        foodMapper.addFood(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
        }

        //往菜品中插入口味
        dishFlavorMapper.insertFlavor(flavors);
    }

    //菜品分页查询
    @Override
    public PageResult selectFood(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<Dish> dishes = foodMapper.selectFood(dishPageQueryDTO);
        List<Dish> result = dishes.getResult();
        long total = dishes.getTotal();

        return new PageResult(total, result);
    }

    //菜品批量删除
    @Override
    //dish_id在三张表中都有dish,dish_flavor,setmeal_dish三张表中,删除一张表中的id肯定不可以
    //要考虑三张表之间的关联性
    public void deleteFood(List<Long> ids) {
        //遍历传进来的ids集合
        //判断当前菜品是不是售卖状态
        //Status==1为启用状态不可以删除
        //先写一个FoodMapper层里的根据id查询方法,获取dish对象,通过对象点出状态码
        for (Long id : ids) {
            Dish dish = foodMapper.dishGetById(id);
            if (dish.getStatus() == 1) {
                System.out.println("该菜品不能删除为启用状态");
                throw new RuntimeException("不能删除是启用状态的菜品");
            }
            {
                //两表查询(dish和setmeal_dish,用dish的id去查询setmeal_dish的信息,如果能查到说明量表关联)
                List<Long> setmealDishes = setmealDishMapper.selectDishById(ids);
                if (setmealDishes != null & setmealDishes.size() > 0) {
                    throw new RuntimeException("该菜品已被关联,请先删除关联");
                }
                {
                    for (Long aLong : ids) {
                        //删除dish里的数据
                        foodMapper.deleteById(aLong);
                        //删除dish_flavor里的数据
                        dishFlavorMapper.deleteByDishId(aLong);
                    }
                }
            }
        }

    }


    //根据id查询菜品(修改前的数据回显)
    @Override
    public DishDTO selectById(Long id) {
        //查出一个dishDTO因为这个对象里有口味的集合,
        DishDTO dishDTO = foodMapper.selectById(id);
        //再写一个查询出来口味集合的mysql语句返回结果是集合
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectDishFlavor(id);
        //将这个集合设置进dishDTO对象里
        dishDTO.setFlavors(dishFlavors);
        return dishDTO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        LocalDateTime now = LocalDateTime.now();
        dish.setUpdateTime(now);
        //修改菜品表基本信息
        foodMapper.update(dish);

        //删除原有的口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        //重新插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertFlavor(flavors);
        }
    }
    //修改菜品售卖状态
    @Override
    public void putStatus(Integer status,Integer id){
        dishFlavorMapper.putStatus(status,id);
    }
    //根据分类ID查询
    @Override
    public List<Dish> selectCategory(Long categoryId) {
        List<Dish> dishes = foodMapper.selectCategory(categoryId);
        return dishes;
    }
}
//修改菜品



